export function formatObjectLiteral(obj) {
	function buildString(o) {
		if (o && typeof o === 'object' && !Array.isArray(o)) {
			const entries = Object.entries(o).map(([key, value]) => {
				const valStr = buildString(value);
				// Only remove quotes for valid JavaScript identifier keys
				const keyStr = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(key) ? key : JSON.stringify(key);
				return `${keyStr}: ${valStr}`;
			});
			return `{${entries.join(',')}}`;
		} else if (Array.isArray(o)) {
			const elements = o.map(buildString);
			return `[${elements.join(',')}]`;
		} else {
			return JSON.stringify(o);
		}
	}

	return `${buildString(obj)}`;
}


export function parseObjectLiteral(str) {
	// 将所有未被引号包围的键加上双引号，以使字符串成为有效的JSON
	const jsonStr = str.replace(/([\w$]+)\s*:/g, '"$1":');

	try {
		// 解析修改后的字符串为对象
		return JSON.parse(jsonStr);
	} catch (e) {
		console.error("Failed to parse the object literal string:", e);
		throw e;
	}
}




export function desensitizeChineseName(fullName) {
	// 对中文名字进行脱敏处理
	if (!fullName || fullName.length <= 1) return fullName;
	return fullName[0] + (fullName.length > 2 ? '**' : '*');
}

// 身份证号码脱敏
export function validateAndDesensitizeIDCard(idCard) {
	let isValid = false;
	let desensitizedIDCard = '';

	// 定义正则表达式以匹配15位或18位身份证号码
	const idCardPattern = /^(^\d{15}$)|(^\d{17}(\d|X|x)$)/;

	if (idCardPattern.test(idCard)) {
		isValid = true;
		// 如果是18位身份证号码，则保留前6位和后4位，中间用星号代替
		if (idCard.length === 18) {
			desensitizedIDCard = idCard.replace(/^(.{6})(?:\d+)(.{4})$/, '$1******$2');
		}
		// 如果是15位身份证号码，则保留前6位和后3位，中间用星号代替
		else if (idCard.length === 15) {
			desensitizedIDCard = idCard.replace(/^(.{6})(?:\d+)(.{3})$/, '$1***$2');
		}
	}

	return {
		isValid,
		desensitizedIDCard
	};
}